package leo.manager
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.system.ApplicationDomain;
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	
	import leo.data.GameModel;

	/**
	 * swf管理器 
	 * @author leo
	 * 
	 */
	public class McMannager
	{
		private static var _instance :McMannager = null;
		public function McMannager()
		{
		}
		/**
		 * 获取mc 
		 * @param name
		 * @return 
		 * 
		 */		
		public function getMCByName(name :String):MovieClip
		{
			var mc :MovieClip;
			if(ApplicationDomain.currentDomain.hasDefinition(name))
			{
				var cls :Class = getDefinitionByName(name) as Class;
				mc = new cls() as MovieClip;
			}else
			{
				trace("have no this swf:" + name);
			}
			
			return mc;
		}
		
//		public function getSPByName(name :String):Sprite
//		{
//			var mc :Sprite;
//			if(ApplicationDomain.currentDomain.hasDefinition(name))
//			{
//				var cls :Class = getDefinitionByName(name) as Class;
//				mc = new cls() as Sprite;
//			}else
//			{
//				trace("have no this swf:" + name);
//			}
//			
//			return mc;
//		}
//		/**
//		 * 获取位图信息 
//		 * @param name
//		 * @return 
//		 * 
//		 */		
//		public function getBitmapByName(name :String):BitmapData
//		{
//			var bmd :BitmapData;
//			var mc :MovieClip = getMCByName(name);
//			bmd = mcToBMD(name);
//			return bmd;
//		}

		/**
		 * swf2bitmapdata 
		 * @param name
		 * @return 
		 * 
		 */		
		public function mcToBMD(name :String):BitmapData
		{
			var bmd :BitmapData;
			if(ApplicationDomain.currentDomain.hasDefinition(name))
			{
				var cls :Class = getDefinitionByName(name) as Class;
				var target :MovieClip = new cls() as MovieClip;
				if(!target) return null;
				target.gotoAndStop(1);
				bmd = draw(target);
			}else
			{
				trace("mcToBMD fail " + name);
			}
			return bmd;
		}
		/**
		 * 多帧的swf转换 
		 * @param name
		 * 
		 */		
		public function swfToBitmapDatas(name :String):void
		{
			if(ApplicationDomain.currentDomain.hasDefinition(name))
			{
				var imgDict :Dictionary = GameModel.instance.imgDict;
				var cls :Class = getDefinitionByName(name) as Class;
				var target :MovieClip = new cls() as MovieClip;
				if(!target) return;
				target.gotoAndStop(1);
				var bmd :BitmapData;
				for(var i :int = 1; i <= target.currentScene.numFrames; i++)
				{
					bmd = draw(target);
					var name :String = target.currentFrameLabel;
					if(!name) 
					{
						trace("swfToBitmapDatas name is null");
						return;
					}
					imgDict[name] = bmd;
					target.nextFrame();
				}
			}else
			{
				trace("not find this swf :" + name);
			}
		}
		
		/**
		 * 画位图 
		 * @param displayObj
		 * @return 
		 * 
		 */		
		private function draw(displayObj:DisplayObject):BitmapData
		{
			var rect:Rectangle = displayObj.getBounds(displayObj);
			var m:Matrix = new Matrix();
//			m.translate(-rect.x,-rect.y);
			var bmd:BitmapData = new BitmapData(rect.width,rect.height,true,0);
			bmd.draw(displayObj,m);
			return bmd;
		}

		public static function get instance():McMannager
		{
			if(_instance == null)
			{
				_instance = new McMannager();
			}
			return _instance;
		}

	}
}